package com.zichan360.Spring5WebFlux.controller;

import com.zichan360.common.BaseResponse;
import com.zichan360.exception.Exception404;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 创建用户:杨辽
 * 创建时间: 2018-03-14 14:16
 * 描   述: 公共controller继承类
 **/
public class DefaultController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private HttpHeaders httpHeaders = new HttpHeaders();

    /**
     * 类 UserController 是具体的 Spring MVC 控制器类。它使用类 UserService 来完成具体的功能。类 UserController 中使用了注解@ExceptionHandler
     * 来添加了 ResourceNotFoundException 异常的处理方法，并返回 404 错误。类 UserController 中的方法都很简单，只是简单地代理给 UserService 中的对应方法。
     */
    @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "404")
    @ExceptionHandler(Exception404.class)
    public void notFound() {

    }

    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<BaseResponse> exceptionHandler(Exception ex) {
        logger.error("{}", ex);
        return new ResponseEntity<>(BaseResponse.buildResponse(false, ex.getMessage(), null), httpHeaders, HttpStatus.OK);
    }

}
